package DesignModeTest;

public class TestProxy {

  public static void main(String[] args) {
    Image image = new ProxyImage("test_fuck.jpg");
    // 图像将从磁盘加载
    image.display();

    // 图像不需要从磁盘加载
    image.display();
  }

}

interface Image{
  void display();
}

class RealImage implements Image {

  private String fileName;

  public RealImage(String fileName) {
    this.fileName = fileName;
    loadFromDisk(fileName);
  }

  private void loadFromDisk(String fileName) {
    System.out.println("Loading" + fileName);
  }

  @Override
  public void display() {
    System.out.println("Displaying" + fileName);
  }
}

class ProxyImage implements  Image {

  private RealImage realImage;

  public String fileName;

  public ProxyImage(String fileName){
    this.fileName = fileName;
  }
  @Override
  public void display() {

    if(realImage == null) {
      realImage = new RealImage(fileName);
    }
    realImage.display();
  }
}
